﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using ARS.PublicTool.DataHandle;

namespace ARS.PublicTool
{
    public class ReflectionHelper
    {
        /// <summary>
        /// 获取类中的属性值
        /// </summary>
        /// <param name="type">type</param>
        /// <param name="fieldName">属性名称</param>
        /// <param name="Model">实体</param>
        /// <returns></returns>
        public static object GetObjAttrValue(Type type, string fieldName, object Model)
        {
            object o = type.GetProperty(fieldName).GetValue(Model, null);
            return o;
        }

        /// <summary>
        /// 设置属性值
        /// </summary>
        /// <param name="typeModel">type</param>
        /// <param name="fieldName">属性名称</param>
        /// <param name="value">属性值</param>
        /// <param name="Model">实体</param>
        public static void SetObjAttrValue(Type typeModel, string fieldName, string value, object Model)
        {
            object objValue;
            Type fieldType = typeModel.GetProperty(fieldName).PropertyType;
            //Type t = typeof (Nullable<>);
            if (fieldType.Name == typeof (Nullable<>).Name)
            {
                objValue = value.ConvertTo<Nullable<int>>();
            }
            else
            {
                objValue = Convert.ChangeType(value, fieldType);
            }
            //object objValue = value.ConvertTo<Nullable<int>>();// Convert.ChangeType(value,fieldType )
            typeModel.GetProperty(fieldName).SetValue(Model, objValue, null);
            //type.GetProperty(fieldName).SetValue(Model,value,null);
        }

        public static PropertyInfo[] GetProperties(Type type)
        {
            return type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
        }
        /// <summary>
        /// 缓存创建对象
        /// </summary>
        /// <param name="AssemblyPath"></param>
        /// <param name="classNamespace"></param>
        /// <returns></returns>
        public static object CreateObjectCache(string AssemblyPath, string classNamespace)
        {
            object objType = WebHelper.DataCache.GetCache(classNamespace);
            if (objType == null)
            {
                try
                {
                    objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
                    if (objType != null)
                    {
                        WebHelper.DataCache.SetCache(classNamespace, objType);
                    }                    
                    return objType;
                }
                catch (System.Exception ex)
                {
                    //string str=ex.Message;// 记录错误日志
                    return null;
                }
            }
            return objType;
        }

        public static T CreateObjectCache<T>(string AssemblyPath, string classNamespace)
        {
            return (T) CreateObjectCache(AssemblyPath, classNamespace);
        }

        public static T CreateObjectCacheConfig<T>(string assemblyPath, string classNamespace)
        {

            assemblyPath = ConfigurationManager.AppSettings[assemblyPath];
            if (string.IsNullOrEmpty(assemblyPath))
            {
                object obj = null;
                return (T)obj;
            }
            else
            {
                classNamespace = ConfigurationManager.AppSettings[classNamespace];
            }

            return CreateObjectCache<T>(assemblyPath, classNamespace);
        }

        /// <summary>
        /// 反射获取对象
        /// </summary>
        /// <param name="AssemblyPath"></param>
        /// <param name="classNamespace"></param>
        /// <returns></returns>
        public static object CreateObjectNoCache(string AssemblyPath, string classNamespace)
        {
            try
            {
                object objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
                return objType;
            }
            catch (System.Exception ex)
            {
                //string str=ex.Message;// 记录错误日志
                return null;
            }

        }

        /// <summary>
        /// 程序集和命名空间
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="assemblyPath"></param>
        /// <param name="classNamespace"></param>
        /// <returns></returns>
        public static T CreateObjectNoCache<T>(string assemblyPath, string classNamespace)
        {

            object objType = Assembly.Load(assemblyPath).CreateInstance(classNamespace);
            //if (objType == null)
            //{
            //    return null;
            //}
            return (T) objType;
        }

        /// <summary>
        /// 从config里面获取
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="assemblyPath">配置程序集</param>
        /// <param name="classNamespace">配置命名空间</param>
        /// <returns></returns>
        public static T CreateObjectNoCacheConfig<T>(string assemblyPath, string classNamespace)
        {

            assemblyPath = ConfigurationManager.AppSettings[assemblyPath];
            //string ClassNamespace = null;
            if (string.IsNullOrEmpty(assemblyPath))
            {
                object obj = null;
                return (T)obj;
            }
            else
            {
                classNamespace = ConfigurationManager.AppSettings[classNamespace];
            }

            return CreateObjectNoCache<T>(assemblyPath, classNamespace);
        }



    }
}
